package day11;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.junit.Test;


public class JDBCTest {
	

	public static void main(String[] args) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			//1、加载驱动Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			//2、获得连接对象Connection
			// Connection一定要选java.sql包下面
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1912?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			//3、写sql语句
			String sql = "select id,name,age,gender from student";
			//4、创建Statement
			statement = connection.createStatement();
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                              executeQuery()
			//           返回值：结果集ResultSet
			resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<Student>();
			// 如果有数据，就指向第一个，而且返回true
			while(resultSet.next()) {
				// 每一次循环拿出表里面的一行数据，然后封装成一个Student对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			// 先打开的后关闭
			if (resultSet != null) {
				try {
					// The local variable resultSet may not have been initialized
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	@Test
	public void testPreparedStatement() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1912?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			String sql = "select id,name,age,gender from student";
			// 得到prepareStatement而且把sql语句传过去
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testPreparedStatement2() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student";
			// 得到prepareStatement而且把sql语句传过去
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	@Test
	public void testPreparedStatement3() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student where name like ?";
			// 得到prepareStatement而且把sql语句传过去
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张%");
			resultSet = preparedStatement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	
}
